﻿using System;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Distribution.Service;
using DistributionApplication.Controls.Events;
using Entities;

namespace DistributionApplication.Controls
{
    public partial class TransformerHomeGrid : UserControl
    {
        private Int32 m_SelectedId = Int32.MinValue;
        public EPole Pole {get;set;}

        public TransformerHomeGrid()
        {
            InitializeComponent();
        }

        public void ConfigureGrid()
        {
            grdTransformers.RowHeadersVisible = false;

            #region Set grid styles
            DataGridTableStyle _GridTableStyle = new DataGridTableStyle();
            DataGridTextBoxColumn _DGCol;
            
            #region PoleId

            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "TransformerId";
            _DGCol.HeaderText = "Transformer";
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            
            #endregion

            #region Number

            _DGCol = new DataGridTextBoxColumn();
            _DGCol.MappingName = "SerialNumber";
            _DGCol.HeaderText = "SerialNumber";
            _DGCol.Width = 183;
            _DGCol.NullText = String.Empty;
            _GridTableStyle.GridColumnStyles.Add(_DGCol);
            
            #endregion

            grdTransformers.TableStyles.Add(_GridTableStyle);
            ReloadGrid();

            #endregion
        }

        public void ReloadGrid()
        {
            DataTable transformers = LTransformer.GetTransformerDetails(Pole);

            grdTransformers.DataSource = transformers;
            
            if (transformers.Rows.Count > 0)
                grdTransformers.Select(0);
        }

        public Int32 GetSelectedInspection()
        {
            if (grdTransformers.CurrentRowIndex >= 0 && grdTransformers.IsSelected(grdTransformers.CurrentRowIndex))
                m_SelectedId = (Int32)grdTransformers[grdTransformers.CurrentRowIndex, 0];
            else
                m_SelectedId = Int32.MinValue;

            return m_SelectedId;
        }

        public ETransformer GetLastInspection()
        {
            ETransformer transformer = null;

            if (grdTransformers.VisibleRowCount > 0) 
            {
                transformer = new ETransformer();
                transformer.Id = (Int32)grdTransformers[grdTransformers.VisibleRowCount - 1, 0];
            }

            return transformer;
        }

        private void grdInspections_CurrentCellChanged(object sender, EventArgs e)
        {
            if (grdTransformers.CurrentRowIndex >= 0)
            {
                Rectangle r = grdTransformers.GetCellBounds(grdTransformers.CurrentRowIndex, 1);
                grdTransformers.Select(grdTransformers.CurrentRowIndex);
                menuGrid.Show(grdTransformers, new Point(r.X, r.Y));
            }
        }

        #region HomeGridEvents

        public event EventHandler<GridEventArgs> ClickedAddTransformer;
        public event EventHandler<GridEventArgs> ClickedEditTransformer;
        public event EventHandler<GridEventArgs> ClickedRemoveTransformer;

        private void OnClickedEditMeter(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedEditTransformer != null)
                ClickedEditTransformer(o, e);
        }

        private void OnClickedRemoveMeter(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedRemoveTransformer != null)
                ClickedRemoveTransformer(o, e);
        }

        private void OnClickedAddMeter(object o, GridEventArgs e)
        {
            // Safely invoke an event:
            if (ClickedAddTransformer != null)
                ClickedAddTransformer(o, e);
        }

        private void mitAddTransformer_Click(object sender, EventArgs e)
        {
            OnClickedAddMeter(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitEditTransformer_Click(object sender, EventArgs e)
        {
            OnClickedEditMeter(sender, new GridEventArgs(GetSelectedInspection()));
        }

        private void mitRemoveTransformer_Click(object sender, EventArgs e)
        {
            OnClickedRemoveMeter(sender, new GridEventArgs(GetSelectedInspection()));
        }

        #endregion
    }
}
